import { call, put } from 'redux-saga/effects';
import { getCaptcha, userLogin } from '../services/user';

export default {
  namespace: 'login',
  state: {
    userInfo: {},
    captcha: '',
    result: {},
  },
  effects: {
    *getCaptcha({ payload }) {
      const response = yield call(getCaptcha, payload);
      yield put({
        type: 'login/gotCaptcha',
        payload: response,
      });
    },
    *userLogin({ payload }) {
      const response = yield call(userLogin, payload);
      yield put({
        type: 'login/dealLogin',
        payload: response,
      });
    },
  },
  reducers: {
    gotCaptcha(state, action) {
      return {
        ...state,
        captcha: action.payload,
      };
    },
    dealLogin(state, action) {
      return {
        ...state,
        result: action.payload,
      };
    },
  },
};
